import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';


class ProgressDialogUtils {

  static bool isProgressVisible = false;

  static dynamic CallackProgressDialog(dynamic cb) async {
    try {
      showProgressDialog();
      return await cb;
    } catch (e) {
      // throw Exception(e);
      hideProgressDialog();
      // Utoast(UtoastStatus.err, "$e");
      Fluttertoast.showToast(
          msg: "$e",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);

    } finally {
      hideProgressDialog();
    }
  }

  ///common method for showing progress dialog
  static void showProgressDialog({isCancellable = false}) {
    if (!isProgressVisible) {
      Get.dialog(
        Center(
          child: CircularProgressIndicator.adaptive(
            strokeWidth: 4,
            valueColor: AlwaysStoppedAnimation<Color>(
              Colors.white,
            ),
          ),
        ),
        barrierDismissible: isCancellable,
      );
      isProgressVisible = true;
    }
  }

  ///common method for hiding progress dialog
  static void hideProgressDialog() {
    if (isProgressVisible) {
      Get.back();
    }
    isProgressVisible = false;
  }

  static void showErrMsg(String msg) {
    hideProgressDialog();
    // Udialog(UtoastStatus.err, msg);
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }
}

